package com.beiye.drivertransport.thematic.learning.examiantion;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.CertificateCompletionBean;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.QRCodeUtil;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.UserManger;
import com.githang.statusbar.StatusBarCompat;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

//2019从业人员专项培训结业证书
public class CertificateCompletionActivity extends TwoBaseAty {

    ImageView img_photo;//头像

    TextView tv_title;

    ImageView img_certificate_back;

    ImageView img_sign;//签名

    TextView tv_name1;//姓名

    TextView tv_id1;//身份证
    //    @Bind(R.id.tv_phone1)
//    TextView tv_phone1;//手机号

    TextView tv_companyname1;//公司名称

    TextView tv_companytype1;//公司种类

    TextView tv_region1;//所属区域

    TextView tv_time1;//学时

    TextView tv_Fraction1;//分数

    TextView tv_dec;

    TextView tv_time4;//盖章下日期

    ImageView img_code;//二维码生成
    private String begindateStr;
    private String enddateStr;
    private String stName;

    @Override
    public int getLayoutId() {
        return R.layout.activity_certificate_completion;
    }


    public void onClick(View view) {
        if (view.getId() == R.id.img_certificate_back) {
            finish();
        }

    }

    @Override
    public void initData() {
        img_photo = (ImageView)findViewById(R.id.img_photo);
        tv_title = (TextView) findViewById(R.id.tv_title);
        img_certificate_back = (ImageView) findViewById(R.id.img_certificate_back);
        img_sign = (ImageView)findViewById(R.id.img_sign);
        tv_name1 = (TextView)findViewById(R.id.tv_name1);
        tv_id1 = (TextView)findViewById(R.id.tv_id1);
        tv_companyname1 = (TextView)findViewById(R.id.tv_companyname1);
        tv_companytype1 = (TextView)findViewById(R.id.tv_companytype1);
        tv_region1 = (TextView)findViewById(R.id.tv_region1);
        tv_time1 = (TextView)findViewById(R.id.tv_time1);
        tv_Fraction1 = (TextView)findViewById(R.id.tv_Fraction1);
        tv_dec = (TextView) findViewById(R.id.tv_dec);
        tv_time4 = (TextView)findViewById(R.id.tv_time4);
        img_code = (ImageView)findViewById(R.id.img_code);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#ffffff"));

    }

    @Override
    public void requestData() {

        Bundle bundle = getIntent().getExtras();
        String orgId = bundle.getString("orgId");
        stName = bundle.getString("stName");
        int stId = bundle.getInt("stId");
        int usch=bundle.getInt("uschSn");


        tv_title.setText(stName);

        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean databean = userInfo.getData();
        String userId = databean.getUserId();
        new Login().getCertificateofCompletionQuery(userId, orgId, stId, this, 1);//结业证书查询


        Bitmap qrCode = QRCodeUtil.createQRCode(AppInterfaceConfig.BASE_URL + "app/spec/toSpecTCert?userId=" + userId + "&orgId=" + orgId + "&stId=" + stId+"&usch="+usch);
        img_code.setImageBitmap(qrCode);

    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if (what == 1) {
            CertificateCompletionBean certificateCompletionBean = JSON.parseObject(result, CertificateCompletionBean.class);
            if (certificateCompletionBean != null) {
                CertificateCompletionBean.DataBean dataBean = certificateCompletionBean.getData();
                String adName = dataBean.getAdName();//所属区域
                String orgName = dataBean.getOrgName();//公司
                String ftName = dataBean.getFtName();//企业种类
                String userName = dataBean.getUserName();//姓名
                String userMobile = dataBean.getUserMobile();//手机号
                String idcNo = dataBean.getIdcNo();//身份证
                String photoUrl = dataBean.getPhotoUrl();//头像
                String signPicUrl = dataBean.getSignPicUrl();//签名
                int chNo = dataBean.getChNo();//学时数
                long beginDate = dataBean.getBeginDate();//开始日期
                long endDate = dataBean.getEndDate();//结束日期
                int score = dataBean.getScore();//分数

                if (photoUrl != null) {
                    Picasso.with(CertificateCompletionActivity.this).load(Uri.parse(photoUrl)).placeholder(R.mipmap.my_head).into(img_photo);//加载图片
                }
                if (userName != null) {
                    tv_name1.setText(userName);
                }
                if (idcNo != null) {
                    tv_id1.setText(idcNo);
                }
//               if(userMobile!=null)
//               {
//                   tv_phone1.setText(userMobile);
//               }
                if (orgName != null) {
                    tv_companyname1.setText(orgName);
                }
                if (ftName != null) {
                    tv_companytype1.setText(ftName);
                }
                if (adName != null) {
                    tv_region1.setText(adName);
                }
                if (signPicUrl != null) {
                    Picasso.with(CertificateCompletionActivity.this).load(Uri.parse(signPicUrl)).placeholder(R.mipmap.no_data).into(img_sign);//加载图片
                }

                if (chNo != 0) {
                    tv_time1.setText(chNo + " 学时");
                }
                if (score != 0) {
                    tv_Fraction1.setText(score + " 分");
                }

                Date begindate1 = new Date(beginDate);
                Date enddate1 = new Date(endDate);
                try {
                    //yyyy表示年MM表示月dd表示日
                    //yyyy-MM-dd是日期的格式，比如2015-12-12如果你要得到2015年12月12日就换成yyyy年MM月dd日
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
                    //进行格式化
                    begindateStr = sdf.format(begindate1);
                    enddateStr = sdf.format(enddate1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                tv_dec.setText("        在" + begindateStr + "至" + enddateStr + "，通过驾运宝交通运输安全生产信息化平台完成" + stName + "，考试合格。");
                tv_time4.setText(enddateStr);


            }

        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (what == 1) {
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            String msg = checkBean.getMsg();
            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CertificateCompletionActivity.this);
            builder1.setMessage(msg);
            builder1.setTitle("提示:");
            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    ;
                    //设置你的操作事项
                }
            });
            builder1.create().show();

        }
        super.onError(result, call, response, what);
    }
}
